import requests
import suiji
from lxml import etree
import man 
# from PIL import Image
url1 = 'https://www.ftest.us/register?aff=8433'
qianzhui = suiji.ranstr(8)   #随机8位邮箱前坠
houzhui = suiji.email()      #随机邮箱后缀
youxiang = qianzhui+houzhui     #随机邮箱
post_url = 'https://www.ftest.us/register'
print(youxiang)
headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'   
    }
def get_url1():
    '''first获取token和验证码链接'''
    resporns = requests.get(url=url1, headers=headers)
    # with open('1.html','w') as f:
    #     f.write(resporns.text)
    # xpath解析获得tokey
    html = etree.HTML(resporns.text)
    # toke = etree.tostring(html).decode('utf-8')
    toke = html.xpath('//input[@name="_token"]/@value')
    # 获取验证码链接
    yanzheng_img_url = html.xpath('//img[@onclick]/@src')
    # print(yanzheng_img)
    return toke,yanzheng_img_url
    # print(toke)
    # print(resporns.text)

toke,yanzheng_img_url = get_url1()
print(toke)
print(yanzheng_img_url)
# token = m[0]
mima = ''
yanzheng = '4'
date = {
    'username': youxiang,
    # 'register_token': 'EpiWy3MhmJKYMpUa',
    '_token': toke[0],
    # 'aff': ''
    'password': youxiang,
    'repassword': youxiang,
    'code': '',
    'captcha': yanzheng
    }

def yanzhenma():
    '''通过验证码链接获取验证码图片并保存为ls.jpg'''
    png_url = yanzheng_img_url[0]
    png = requests.get(url=png_url,headers=headers)
    yanzhengma = png.content
    with open("ls.jpg",'wb') as f:
        f.write(yanzhengma)
def denglu_post():
    '''post请求注册账号'''
    print('正在注册请稍后')

    rrebonse = requests.post(post_url, data=date, headers=headers)
    with open('1.html','w') as f:
        f.write(rrebonse.text)

if __name__ == '__main__':
    yanzhenma()
    get_url1()
    denglu_post()
    